//
//  ZYXPlistViewController.m
//  OCDataStore
//
//  Plist 存储
//

#import "ZYXPlistViewController.h"
#import "ZYXPlistPerson.h"

@interface ZYXPlistViewController ()
- (IBAction)save;
- (IBAction)read;
@end

@implementation ZYXPlistViewController

- (IBAction)save {
    [self saveArray];
}

- (void)saveArray
{
    // 1.获得沙盒根路径
    NSString *home = NSHomeDirectory();
    
    // 2.document路径
    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
    
    // 3.新建数据
//    ZYXPlistPerson *p = [[ZYXPlistPerson alloc] init];
//    p.name = @"rose";
//    p.title = @"喜欢白雪公主";
//    
//    NSArray *data = @[p];
    
    NSArray *data = @[@"jack", @10, @"rose"];
    
    //如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型，就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中

    
    NSString *filepath = [docPath stringByAppendingPathComponent:@"zyxplistdata.plist"];
    
    
    [data writeToFile:filepath atomically:YES];
//已经将归档的plist文件拷贝到了Plist目录中 以供参阅
    
    
//    <?xml version="1.0" encoding="UTF-8"?>
//    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
//    <plist version="1.0">
//    <array>
//    <string>jack</string>
//    <integer>10</integer>
//    <string>rose</string>
//    </array>
//    </plist>
    
}

- (IBAction)read {
    // 1.获得沙盒根路径
    NSString *home = NSHomeDirectory();
    
    // 2.document路径
    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
    
    // 3.文件路径
    NSString *filepath = [docPath stringByAppendingPathComponent:@"zyxplistdata.plist"];
    
    // 4.读取数据
    NSArray *data = [NSArray arrayWithContentsOfFile:filepath];
    
    ZYXLog(@"%@", data);
}

@end
